package com.zw.yzk.kabu.view.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;

import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.base.BaseActivity;
import com.zw.yzk.kabu.constant.ActivityConstant;
import com.zw.yzk.kabu.management.ToolbarBuilder;

import butterknife.Bind;

/**
 * Created by wei on 2016/2/23.
 */
public class WebActivity extends BaseActivity {

    @Bind(R.id.webview)
    WebView webView;
    @Bind(R.id.loading)
    ImageView loading;

    public static void startWebActivity(Context context, String title, String url) {
        Intent intent = new Intent(context, WebActivity.class);
        intent.putExtra(ActivityConstant.OPEN_WEB_URL, url);
        intent.putExtra(ActivityConstant.OPEN_WEB_TITLE, title);
        context.startActivity(intent);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_web;
    }

    @Override
    public void iniToolbar(BaseActivity activity) {
        String title = getIntent().getStringExtra(ActivityConstant.OPEN_WEB_TITLE);
        ToolbarBuilder
                .createBuilder(this)
                .setTitle(title)
                .build();
    }

    @Override
    public void initComponent() {

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initWebView();
        starAnimation();

        webView.loadUrl(getIntent().getStringExtra(ActivityConstant.OPEN_WEB_URL));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        if(webView != null) {
            webView.destroy();
        }
    }

    private void initWebView() {
        //设置 缓存模式
        webView.getSettings().setDomStorageEnabled(true);
        webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
        webView.getSettings().setAppCachePath(getCacheDir().getAbsolutePath());
        webView.getSettings().setDefaultTextEncodingName("utf-8");
        webView.getSettings().setAllowFileAccess(true);
        webView.getSettings().setAppCacheEnabled(true);
        //缩放以适应屏幕
//        webView.getSettings().setUseWideViewPort(true);
//        webView.getSettings().setLoadWithOverviewMode(true);
        //设置WebView属性，能够执行JavaScript脚本
        webView.getSettings().setJavaScriptEnabled(true);

        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                stopAnimation();
            }

            @Override
            public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
                super.onReceivedError(view, request, error);
                stopAnimation();
            }
        });
    }

    private void starAnimation() {
        if (loading.getVisibility() == View.GONE) {
            loading.setVisibility(View.VISIBLE);
            RotateAnimation animation = new RotateAnimation(0f, 360f,
                    Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
                    0.5f);
            animation.setDuration(1500);
            animation.setInterpolator(new LinearInterpolator());
            animation.setRepeatCount(Animation.INFINITE);
            animation.setFillAfter(true);
            loading.startAnimation(animation);
        }
    }

    private void stopAnimation() {
        if (loading == null) {
            return;
        }
        loading.clearAnimation();
        loading.setVisibility(View.GONE);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack()) {
            webView.goBack();// 返回前一个页面
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }


}
